package com.stepforward.me;


import java.util.ArrayList;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Route {

	private ArrayList<Itinerary> itineraries = new ArrayList<Itinerary>();
	private Itinerary itinerary = new Itinerary();
	private SuggestedLocation suggestion;

	public SuggestedLocation getSuggestion() {
		return suggestion;
	}

	public boolean getRoute(String departure, String arrival, String month,
			String day, String year, String hour, String minute, String pm)
			throws Exception {

		Transfer transfer = new Transfer();
		String url = "http://www.portauthority.org/atisdnn/default.aspx?";

		
		String twenty = "%20";
		String space = " ";
		if (!departure.equals("")) {
			url += "departure=" + departure.replaceAll(space, twenty);
		}
		if (!arrival.equals("")) {
			url += "&arrival=" + arrival.replaceAll(space, twenty);
		}
		if (!month.equals("")) {

			url += "&month=" + month;
		}
		if (!day.equals("")) {
			url += "&day=" + day;
		}
		if (!year.equals("")) {
			url += "&year=" + year;
		}
		if (!hour.equals("")) {
			url += "&hour=" + hour;
		}
		if (!minute.equals("")) {
			url += "&minute=" + minute;
		}
		if (!pm.equals("")) {
			url += "&pm=" + pm;
		}
		
		System.out.println("" + url);
		try {
		Connection conn = Jsoup.connect(url);
		Document doc = conn.get();
		Elements divs = doc.select("div[id]");
		
		Elements selects = doc.select("select[name]");
		suggestion = new SuggestedLocation();
		
		for (Element selectElement : selects ){
		    if (selectElement.attr("name").equals("lstClarifyDeparture")|| selectElement.attr("name").equals("lstClarifyArrival")) {
				if (selectElement.attr("name").equals("lstClarifyDeparture")) suggestion.setType("Departure");
				else suggestion.setType("Arrival");
				ArrayList <String> temp = new ArrayList<String>();
				Elements options = selectElement.select("option[value]");
				for (Element option : options){
					temp.add(option.text());
				}
				suggestion.setSuggestions(temp);
				return false;
			}
			    
		}
		
		
		for (Element div : divs) {
			if (div.id().contains("Itinerary")) {
				Elements spans = div.select("span[id]");
				for (Element span : spans) {

					if (span.id().contains("ResultsHeader")) {
						itinerary = new Itinerary();
						itinerary.setHeader(span.text());

					}

					if (span.id().contains("lblRoute")
							&& !span.id().contains("lblRouteID")) {
						transfer = new Transfer();
						transfer.setRoute(span.text());
					}
					if (span.id().contains("lblGetOn")) {
						transfer.setGetOn(span.text());
					}
					if (span.id().contains("lblOnTime")) {
						transfer.setOnTime(span.text());
					}
					if (span.id().contains("lblGetOff")) {
						transfer.setGetOff(span.text());
					}
					if (span.id().contains("lblOffTime")) {
						transfer.setOffTime(span.text());
						itinerary.addTransfer(transfer);
					}
					if (span.id().contains("lblTripTime")) {
						itinerary.setTotalTime(span.text());
						System.out.println(itinerary.getTransfers().size());
						itineraries.add(itinerary);
					}

				}

			} 

		}
		conn.timeout(0);
		
		} catch (Exception e) {
		}
		return true;
	}

	public ArrayList<Itinerary> getItineraries() {
		return itineraries;
	}

	public static void main(String[] args) {
		Route r = new Route();
		try {
			r.getRoute("Ellsworth ave at aiken ave", "5th ave at morewood",
					"4", "26", "2011", "19", "32", "pm");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
